<?php
/**
 * Created by PhpStorm.
 * User: shkj
 * Date: 2020/7/7
 * Time: 13:53
 */

namespace app\api\controller\v1;

use think\Db;
use think\facade\Request;

class Seckill extends Base
{

    public function seckill(){
        $data = Request::post();

        $token = Request()->header('Token');


        if(isset($data['uid'])&&!empty($data['uid'])&&$data['uid'] != 'undefined' && !empty($token)){
            $uid = $data['uid'];
            $status = $this->verifyToken($uid,$token);
        
              if($token && $status){
                
               model('seckill')->order($data);

        }
    }else{
            return_json(40001,'token过期了',40001);
                    }
      
    }


    //秒杀商品队列添加
    private function redisinit($info){
      
   
        $store=$info['sk_num']; // 库存50
        $key = $info['seckill_id']."_goods_".$info['goods_id'];
//        $store = Request::param('store');
        redis()->del($key); // 删除库存列表
        $res=redis()->llen($key); //返回库存长度，这里已经是0
        $count=$store-$res;
        for($i=0;$i<$count;$i++){
            redis()->lpush($key,1); //列表推进50个，模拟50个商品库存
        }

    }

    //秒杀商品
    public function goods()
    {

        //秒杀时间段id
            $skId = Request::param('id');


        //查询已经选中的秒商品 用于列表
        $seckillGoodsRes = db('seckill_goods')->alias('sg')->field('sg.*, g.goods_title, g.goods_price')->join("goods g", "sg.goods_id = g.id")->where('seckill_id',$skId)->order('goods_id asc')->select();
     
        if(request()->isPost()){
                //删除原来的数据
                // db('seckill_goods')->where('seckill_id' , $skId)->delete();
                $data = input('post.');
                if(!empty($data)){
                foreach ($data as $k => $v) {
                   db('seckill_goods')->where('seckill_id' , $skId)->where('goods_id',$v['id'])->delete();
                    $agoods = array(
                        'seckill_id'=>$skId,
                        'goods_id'=>$v['id'],
                        'sk_num'=>$v['sk_num'],
                        'sk_price'=>$v['sk_price'],
                        'limit_num'=>$v['limit_num']
                        );
                    $add = db('seckill_goods')->insert($agoods);
                     $info = array(
                     'sk_num'=>$v['sk_num'],
                     'goods_id'=>$v['id'],
                     'seckill_id'=>$skId
                        );
                      $this->redisinit($info);
                  
                }
                   }else{
                    return_json(400,'请选择秒杀商品！',false);
                   }
                   if($add){
                      return_json(200,'添加秒杀商品成功！',true);
                   }else{
                     return_json(400,'添加秒杀商品失败！',false);
                   }
            }

          
           return_json(200,'成功',$seckillGoodsRes);

        }

        //获取商品列表
        public function selgoods(){
              $pagesize = Request::param('pagesize');
              $pagenum = Request::param('pagenum');
              
        model('seckill')->selgoods($pagesize,$pagenum);
        }

        //删除秒杀商品
        public function delgoods(){
            $gid = Request::param('gid');
            $id = Request::param('id');
            model('seckill')->delgoods($gid,$id);

        }




    public function all(){

        $res = model('seckill')->where('status',1)->order('recommended desc')->all([],'seckill');
        foreach ($res as $key=>$value){
            foreach ($value['seckill'] as $k=>$v){
                $v['goods'] = db('goods')->where('id',$v['goods_id'])->find();
                $v['spec'] = explode(",", $v['spec']);
                foreach ($v['spec'] as $key => $value) {
                    $spec = db('goods_attr')->field('attr_value')->where('id',$value)->find();
                    $g_spec[] = $spec['attr_value'];
                   
                }
              
               
                 $v['spec']= $g_spec; 

                 $v['g_spec']=implode(",",$g_spec);

                 $g_spec = [];
            
            }


        }

        return_json(200,'成功',$res);

        // $this->assign([

        //     'res'=>$res
        // ]);
        // return view('all');
    }

    //后台秒杀管理
    public function select(){
        $pagenum = Request::param('pagenum');
        $pagesize = Request::param('pagesize');
        model('seckill')->select($pagenum,$pagesize);
    }

    public function add(){
        $data = Request::post();
        model('seckill')->add($data);
    }

    public function delete(){
        $id = Request::param('id');
        model('seckill')->del($id);
    }
     public function delAll($ids){
          $ids = Request::param('ids/a');
        model('seckill')->delAll($ids);
   }

   public function detail(){
        $id = Request::param('id');
        model('seckill')->detail($id);
   }

   public function edit(){
      $id = Request::param('id');
      $data = Request::post();
        model('seckill')->edit($id,$data);
   }

   public function status(){
    $id = Request::param('id');
    $status = Request::param('status');
    model('seckill')->status($id,$status);
   }

   //前台秒杀详情
   public function goodsDetail(){
    $id = Request::param('id');
    model('seckill')->goodsDetail($id);
   }

}